﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.HU;
using HiStaff.Util;
using HiStaff.Dal;

namespace HiStaff.iProfile
{
    public partial class BussinessTrip : FormBase
    {
        public BussinessTrip()
        {
            InitializeComponent();
        }

        protected override bool FormLoad()
        {
            try
            {
                AddBasicCommandBar();
                SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
                BaseStatus = Util.Enum.FormStatus.View;
                SetStatusInputControl(this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        private void BindData()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_BUSSINESSTRIP>("APRS_HU_BUSSINESSTRIP", new EMPLOYEE_SEARCH { EMPLOYEEID = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID")) });
                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void BindDataByEmp()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_EMPLOYEE>("PRS_HU_EMPLOYEE", new EMPLOYEE_SEARCH { ORGID = trvOrg.OrgId, TERMINATE = hiCheckBox2.Value });
                grdEmpList.DataSource = lstEmployee;
                grdEmpList.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void trvOrg_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            BindDataByEmp();
        }

        private void grdEmpList_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (grdEmpList.GetValue("ID") == null)
                {
                    grdMain.DataSource = null;
                    grdMain.Refresh();
                    ResetInputControl(this);
                }
                else
                {
                    BindData();
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void grdMain_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                BindDataFromGridToForm(grdMain, this);
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void hiCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (hiCheckBox1.Checked)
                {
                    hiEmployeeSelection1.SelectMultiEmployee = true;
                }
                else
                {
                    hiEmployeeSelection1.SelectMultiEmployee = false;
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void hiCheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            BindDataByEmp();
        }

        protected bool CheckData()
        {
            try
            {
                if (!Common.CheckEffectDateAndExpireDate(hiDatePicker1, hiDatePicker2))
                {
                    MessageBox.Show(GetMessageById("MSG_INVALID_DATE"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool SaveButtonClick()
        {
            try
            {
                if (!CheckData())
                {
                    return false;
                }
                else
                {
                    var objSave = new HU_BUSSINESSTRIP();
                    ValueControlsToObject(objSave, this);
                    if (BaseStatus == Util.Enum.FormStatus.Add)
                    {
                        objSave.ID = 0;
                    }
                    OracleHelper.ExcuteNonQuery("APRU_HU_BUSSINESSTRIP", objSave);

                    objSave.TRIPID = objSave.ID;
                    objSave.EMPLOYEEID = hiEmployeeSelection1.EmployeeSelected[0].ID;
                    OracleHelper.ExcuteNonQuery("APRD_HU_BUSSINESSEMPLOYEE", objSave);
                    foreach (var emp in hiEmployeeSelection1.EmployeeSelected)
                    {
                        objSave.EMPLOYEEID = emp.ID;
                        OracleHelper.ExcuteNonQuery("APRU_HU_BUSSINESSEMPLOYEE", objSave);
                    }
                    BindData();
                    MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool DeleteButtonClick()
        {
            if (grdMain.SelectedItems.Count > 0)
            {
                var row = grdMain.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (OracleHelper.ExcuteNonQuery("APRD_HU_BUSSINESSTRIP", row.GetRow().DataRow))
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        return true;
                    }
                }
            }
            return false;
        }

        protected override bool CancelButtonClick()
        {
            BindDataFromGridToForm(grdMain, this);
            return true;
        }

        private void TotalCost()
        {
            try
            {
                hiNumericTextBox5.Value = hiNumericTextBox1.Value + hiNumericTextBox3.Value + hiNumericTextBox2.Value + hiNumericTextBox4.Value;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void hiNumericTextBox1_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                TotalCost();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void hiNumericTextBox3_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                TotalCost();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void hiNumericTextBox2_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                TotalCost();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void hiNumericTextBox4_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                TotalCost();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }



    }
}
